Valid expressions in ScriptX include things that you might not consider to be expressions in other languages, such as loops and conditionals. Since every construct in ScriptX yields a value, you can write more flexible code by nesting expressions within other expressions and passing around their results.
When a ScriptX expression is evaluated, the resulting value is
always an object, and that object is an instance of a class
(either one of the ScriptX core classes or a class you have
defined). Where the distinction between the value and the
class of an object is important, this book refers to each
separately. For example, the expression 2
+
2
yields an
ImmediateInteger
object whose value is
4
.
For example, you could write the expression a <
b
as follows:
a <
b
In this example, the end of the expression has not yet occurred at the end of the first line, so ScriptX looks to the next line for the expression to be completed. That same expression could not be written this way:
a
< b
Because a
is a complete expression in ScriptX,
the end-of-line after a
is considered the end of
the expression. The second line would then result in an error,
since the ScriptX bytecode compiler expects to find a new
expression beginning on that line.
A sentence is a complete syntactic construct that can be evaluated by the ScriptX bytecode interpreter. The bytecode interpreter does not evaluate an expression until you enter a "complete sentence" in the Listener window. An incomplete sentence can be continued on the following line. The following generalizations apply to completion of ScriptX sentences.
->
).
\
) to indicate
all line breaks, even in cases where it is not required by
ScriptX syntax. Most programmers break lines only in obvious
places, such as after separators and binary operators.A script cannot be broken in the middle of a literal (such as a number or string) without causing a syntax error, or changing the meaning of the expression.
-- this example generates a syntax error
global myArray := #(33, 44, 55, 6
6, 77, 88, 99)
-- ** Syntax error: ("syntax error") at "6" (SyntaxError)
In the next example, the line break is interpreted as a newline character:
global myString := "This is my
string"
"This is my
string"
To force evaluation of an incomplete sentence in the scripter,
type !!
and press enter. Two
exclamation marks act as a terminator, allowing you to begin
entering a new expression. If the expression that the scripter
was currently evaluating is incomplete, termination reports a
syntax error.
(a + b) < !!
-- ** Syntax error: ("syntax error") at "(a + b) < !" (SyntaxError)
For more information on ScriptX syntax and expressions, see Appendix A, "ScriptX Reference."
\
) at the end of the line. A backslash indicates that an expression continues on the next line. The backslash itself is treated as white space. If you prefer, you can use a backslash wherever there is a line break, for readability. When an example in this book uses the backslash, the second line is indented to show continuation from the previous line.
1 + 2 + 3 \
+ 4 + 5
Since tabs are treated as blank space in ScriptX, you can use tabs freely to make your ScriptX code more readable. One place you might not want to use a tab is in a string-you wouldn't want tab characters embedded in your strings.
print "This is an example of a very long expression, one that \
is too long to fit on one line."
The example above shows a call to the print
function. Function calls are the most common case of an
expression that is too long to fit on one line but cannot be
split over multiple lines unless you use a backslash. Function
calls are described in further detail in Chapter 3, "Working with Objects."
Finally, multiple expressions can be placed on one line, separated by semicolons:
t := b * b; if t < j then t else j; print b; print t
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.